package com.testin.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import java.io.File;

/**
 * @author lizhongbin
 * @since 12-09-29
 */
public class SendMultiPartEmail {


    private String hostName;
    private String userName;
    private String password;

//    private MultiPartEmail email;
    private HtmlEmail email;

    public SendMultiPartEmail(String hostName, String userName, String password) {
        if (StringUtils.isBlank(hostName)) {
            throw new IllegalArgumentException("hostName is null error.");
        }
        if (StringUtils.isBlank(userName)) {
            throw new IllegalArgumentException("userName is null error.");
        }
        if (StringUtils.isBlank(password)) {
            throw new IllegalArgumentException("password is null error.");
        }
        this.email = new HtmlEmail();
        this.email.setDebug(true);
        this.hostName = hostName;
        this.userName = userName;
        this.password = password;
        email.setHostName(hostName);
        email.setAuthentication(userName, password);
        email.setDebug(true);
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
        email.setHostName(hostName);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
        email.setAuthentication(this.userName, this.password);
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        email.setAuthentication(this.userName, this.password);
    }

    public void setFrom(String from) {
        try {
            //this.email.setFrom(from);
            this.email.setFrom(from,this.userName);
        } catch (EmailException e) {
           
        }
    }

    public void addSendTo(String sendTo) {
        try {
            email.addTo(sendTo);
        } catch (EmailException e) {
            
        }
    }

    public void addCc(String cc) {
        try {
            email.addCc(cc);
        } catch (EmailException e) {
            
        }
    }

    public void setSubject(String subject) {
        email.setSubject(subject);
    }

    public void setMessageBody(String messageBody) {
        try {
//            email.setMsg(messageBody);
        	email.setCharset("UTF-8");
        	email.setHtmlMsg(messageBody);
        } catch (EmailException e) {
          
        }
    }

    public void addAttachment(String path) {
        EmailAttachment attachment = new EmailAttachment();
        attachment.setName("Attachment");
        attachment.setDescription("Attachment Desc");
        attachment.setPath(path);
        try {
            this.email.attach(attachment);
        } catch (EmailException e) {
           
        }
    }

    public void addAttachment(File file) {
        EmailAttachment attachment;
        attachment = new EmailAttachment();
        attachment.setName(file.getName());
        attachment.setDescription(file.getName());
        attachment.setPath(file.getAbsolutePath());
        try {
            this.email.attach(attachment);
        } catch (EmailException e) {
            
        }
    }

    public String send() throws EmailException {
        return email.send();
    }
}
